package org.afun.oldhand.forum.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.afun.oldhand.R;
import org.afun.oldhand.forum.javaBean.BottomBase;
import org.afun.oldhand.forum.javaBean.BottomType1;
import org.afun.oldhand.forum.javaBean.BottomType3;
import org.afun.oldhand.forum.javaBean.ItemBottomContentBean;
import org.afun.oldhand.forum.javaBean.ItemClassiferBean;
import org.afun.oldhand.forum.javaBean.ItemTopBean;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ForumBottomItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ItemClassiferBean> rData;
    private List<BottomBase> data;
    private Context context;
    private ForumBottomItemAdapter.OnForumBottomItemRecyclerItemClickListener onForumButtomItemRecyclerItemClickListener;

    public enum ItemType{
        ITEM1,ITEM2,ITEM3
    }

    public ForumBottomItemAdapter(List<BottomBase> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = View.inflate(context, R.layout.forum_listitem2, null);
//        return new ForumBottomItemAdapter.ForumBottomItemViewHolder(view);
        if(viewType==ItemType.ITEM1.ordinal()){
            View view= LayoutInflater.from(context).inflate(R.layout.forum_listitem3,parent,false);
            return new ForumBottomItemAdapter.ForumBottomItemViewHolder1(view);
        }else if(viewType==ItemType.ITEM2.ordinal()){
            View view= LayoutInflater.from(context).inflate(R.layout.forum_listitem4,parent,false);
            return new ForumBottomItemAdapter.ForumBottomItemViewHolder2(view);
        }else{
            View view=View.inflate(context,R.layout.forum_listitem2,null);
            return new ForumBottomItemAdapter.ForumBottomItemViewHolder3(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ForumBottomItemViewHolder1){
            BottomBase bottomBase = data.get(position);
            BottomType1 bt1=(BottomType1)bottomBase;
            List<String> texts = bt1.getTexts();
            this.rData=new ArrayList<>();
            for(int i=0;i<texts.size();i+=1){
                this.rData.add(new ItemClassiferBean(texts.get(i)));
            }
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this.context,
                    LinearLayoutManager.VERTICAL,false);
            ((ForumBottomItemViewHolder1)holder).recyclerView.setLayoutManager(linearLayoutManager);
            ((ForumBottomItemViewHolder1)holder).recyclerView.setAdapter(
                    new ForumBottomClassiferItemAdapter(rData,this.context));
            ((ForumBottomItemViewHolder1)holder).type1LoadImg.setImageResource(R.drawable.forum_loadimg);
            ((ForumBottomItemViewHolder1)holder).type1Title.setText("精选分类");

        }else if(holder instanceof ForumBottomItemViewHolder2){
//            ((ForumBottomItemViewHolder2)holder).type2SrcImg.setImageResource(R.drawable.forum_type2src);
//            ((ForumBottomItemViewHolder2)holder).type2title.setText("假期出行游记");
//            ((ForumBottomItemViewHolder2)holder).type2text.setText("3577位车友在晒图");
        }else{
            BottomBase bb=data.get(position);
            if(bb.getType()==3){
                BottomType3 bt3=(BottomType3)bb;
                System.out.println(bt3);
                ((ForumBottomItemViewHolder3)holder).forumTextTitle.setText(bt3.getTitle());
                ((ForumBottomItemViewHolder3)holder).forumUserName.setText(bt3.getNickName());
                ((ForumBottomItemViewHolder3)holder).forumRead.setText(bt3.getScanCount());
            }else{
                // use defalut
                ((ForumBottomItemViewHolder3)holder).forumTextTitle.setText("实现基础的滑动之后实现基础的滑动之后");
            }

        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public int getItemViewType(int position){
        if(position==0){
            return ItemType.ITEM1.ordinal();
        }else if(position==2){
            return ItemType.ITEM2.ordinal();
        }else{
            return ItemType.ITEM3.ordinal();
        }
    }

    //  "精选分类" 的holder type=1
    public class ForumBottomItemViewHolder1 extends RecyclerView.ViewHolder{
        private TextView type1Title;
        private ImageButton type1LoadImg;
        private RecyclerView recyclerView;
        public ForumBottomItemViewHolder1(@NonNull View itemView) {
            super(itemView);
            type1Title=itemView.findViewById(R.id.type1Title);
            type1LoadImg=itemView.findViewById(R.id.type1LoadImg);
            recyclerView=itemView.findViewById(R.id.forumBottomItemClassfied);

        }
    }

    //  "假期出行游记" 的holder type=2
    public class ForumBottomItemViewHolder2 extends RecyclerView.ViewHolder{
//        private ImageView type2SrcImg;
//        private TextView type2title;
//        private TextView type2text;
        public ForumBottomItemViewHolder2(@NonNull View itemView) {
            super(itemView);
//            type2SrcImg=itemView.findViewById(R.id.type2SrcImg);
//            type2title=itemView.findViewById(R.id.type2title);
//            type2text=itemView.findViewById(R.id.type2text);
        }
    }

    // 内容区的holder type=3
    public class ForumBottomItemViewHolder3 extends RecyclerView.ViewHolder {
        ImageView forumBottomContentImg;
        TextView forumTextTitle;
        ImageView forumUserHeadImg;
        TextView forumUserName;
        TextView forumRead;
        public ForumBottomItemViewHolder3(@NonNull View itemView) {
            super(itemView);
            forumBottomContentImg=itemView.findViewById(R.id.forumBottomContentImg);
            forumTextTitle=itemView.findViewById(R.id.forumTextTitle);
            forumUserHeadImg=itemView.findViewById(R.id.forumUserHeadImg);
            forumUserName=itemView.findViewById(R.id.forumUserName);
            forumRead=itemView.findViewById(R.id.forumRead);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onForumButtomItemRecyclerItemClickListener!=null){
                        onForumButtomItemRecyclerItemClickListener.onItemRecyclerItemClick(getAdapterPosition());
                    }
                }
            });

        }
    }

    public void setOrderItemClickListener(ForumBottomItemAdapter.OnForumBottomItemRecyclerItemClickListener listener){
        onForumButtomItemRecyclerItemClickListener=listener;
    }

    public interface OnForumBottomItemRecyclerItemClickListener{
        void onItemRecyclerItemClick(int position);
    }
}
